package com.ywdnf.gomoku.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.util.StringUtils;

/**
 * cookie 工具类
 *
 * @author lss
 * @date 2019-06-28, Fri
 */
public class CookieUtil {

  /**
   * 获取cookie的值
   *
   * @param request http请求
   * @param key cookie key
   * @return cookie value
   */
  public static String getCookies(HttpServletRequest request, String key) {
    if (request == null || StringUtils.isEmpty(key)) {
      return null;
    }
    Cookie[] cs = request.getCookies();
    if (cs != null && cs.length > 0) {
      for (Cookie c : cs) {
        if (c.getName().equals(key)) {
          return c.getValue();
        }
      }
    }
    return null;
  }


  public static void setCookie(HttpServletResponse response, String key, String value) {
    Cookie cookie = new Cookie(key, value);
    cookie.setMaxAge(3600);
    cookie.setPath("/");
    response.addCookie(cookie);
    // return
  }
}
